{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fc23da68",
   "metadata": {},
   "source": [
    "# hvplot.plotting.parallelcoordinates\n",
    "\n",
    "```{eval-rst}\n",
    ".. currentmodule:: hvplot.plotting\n",
    "\n",
    ".. autofunction:: parallel_coordinates\n",
    "```\n",
    "\n",
    "## Examples\n",
    "\n",
    "### Basic parallel coordinates plot\n",
    "\n",
    "This example shows how to create a simple parallel coordinates plot from a dataframe with 4 features and a categorical column."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ded5daa4-c9e1-4c4d-9b5a-835d2f948ea3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "np.random.seed(42)\n",
    "df = pd.DataFrame({\n",
    "    'feature_1': np.random.rand(10) * 20,\n",
    "    'feature_2': np.random.rand(10) * 10,\n",
    "    'feature_3': np.random.randint(0, 100, 10),\n",
    "    'feature_4': np.random.normal(0, 1, 10),\n",
    "    'class': np.random.choice(['A', 'B', 'C'], 10)\n",
    "})\n",
    "\n",
    "hvplot.plotting.parallel_coordinates(df, class_column='class')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "122c7200-886f-4df2-ac77-74dae2cd35f2",
   "metadata": {},
   "source": [
    "### Example with penguins\n",
    "\n",
    "In this example we use 4 features from the *penguins* dataset and analyze how they are related with their `species`. We can see, for instance, that Gentoo penguins dominate the `flipper_length_mm` feature, having the highest flipper lengths.\n",
    "\n",
    ":::{note}\n",
    "It is important to normalize the features before plotting them. This example leverages `scikit-learn` and its [`MinMaxScaler`](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MinMaxScaler.html) transform.\n",
    ":::"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f6c16e2-0213-48bd-b426-68dbe9e6f964",
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot\n",
    "import pandas as pd\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "\n",
    "df = hvplot.sampledata.penguins(\"pandas\")\n",
    "df_scaled = df\n",
    "cols = [\"bill_length_mm\", \"bill_depth_mm\", \"flipper_length_mm\", \"body_mass_g\"]\n",
    "scaler = MinMaxScaler()\n",
    "scaled_features = scaler.fit_transform(df[cols])\n",
    "df_scaled = pd.DataFrame(scaled_features, columns=cols)\n",
    "df_scaled[\"species\"] = df[\"species\"]\n",
    "\n",
    "hvplot.plotting.parallel_coordinates(df_scaled, class_column=\"species\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21d55163-3ecc-42db-9484-2e71c4c5b782",
   "metadata": {},
   "source": [
    "### With Matplotlib\n",
    "\n",
    "Parallel coordinates plots can quickly become pretty large and slow to explore with the Bokeh plotting backend. This example shows how to render such a plot with Matplotlib."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20a8eff0-bd16-426b-be37-963a6dcd1065",
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot\n",
    "import pandas as pd\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "hvplot.extension(\"matplotlib\")\n",
    "\n",
    "df = hvplot.sampledata.penguins(\"pandas\")\n",
    "cols = [\"bill_length_mm\", \"bill_depth_mm\", \"flipper_length_mm\", \"body_mass_g\"]\n",
    "scaler = MinMaxScaler()\n",
    "scaled_features = scaler.fit_transform(df[cols])\n",
    "df_scaled = pd.DataFrame(scaled_features, columns=cols)\n",
    "df_scaled[\"species\"] = df[\"species\"]\n",
    "\n",
    "hvplot.plotting.parallel_coordinates(df_scaled, class_column=\"species\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21d596a0-7eb4-42d4-9fbb-92fd4743a2bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "hvplot.output(backend=\"bokeh\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cce27221-c353-4e4e-9827-fb613f4954f7",
   "metadata": {},
   "source": [
    "### Customize\n",
    "\n",
    "`parallel_coordinates` offers multiple options to customize the plot, with `cols`, `var_name`, `value_name`, `cmap` and `alpha`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bbe3f740-a0ed-46f0-bd4b-5ea60bbad0b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "np.random.seed(42)\n",
    "df = pd.DataFrame({\n",
    "    'feature_1': np.random.rand(10) * 20,\n",
    "    'feature_2': np.random.rand(10) * 10,\n",
    "    'feature_3': np.random.randint(0, 100, 10),\n",
    "    'feature_4': np.random.normal(0, 1, 10),\n",
    "    'class': np.random.choice(['A', 'B', 'C'], 10)\n",
    "})\n",
    "\n",
    "hvplot.plotting.parallel_coordinates(\n",
    "    df, class_column='class', cols=['feature_2', 'feature_3', 'feature_4'],\n",
    "    var_name='Species', value_name='Scaled', cmap='Set1', alpha=0.8,\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
